package cn.hl.hm.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.hl.hm.entity.DriverAmountInfo;
import cn.hl.hm.entity.WithdrawApply;
import cn.hl.hm.service.WithdrawApplyService;
import cn.hl.hm.util.AppPager;
import cn.hl.hm.util.ExportData;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;

@Controller
@RequestMapping("/withdrawApplyController")
public class WithdrawApplyController {
	private static final Logger logger = LoggerFactory.getLogger(WithdrawController.class);
	private final static SimpleDateFormat hhmmSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private final static SimpleDateFormat hhmmSdf1 = new SimpleDateFormat("yyyyMMddHHmm");
	
	@Autowired
	private WithdrawApplyService withdrawApplyService;
	
	/**
	 * 退款记录查询
	 * @param withdrawApply
	 * @param request
	 * @param response
	 * @return 山治
	 * @throws IOException
	 */
	@RequestMapping(value = "/selectWithdrawApply", method = RequestMethod.POST)
	public String selectWithdrawApply( WithdrawApply withdrawApply,
			@RequestParam Integer pageId,
			@RequestParam Integer pageCount,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("退款记录---withdrawApplyController/selectWithdrawApply");
		request.setCharacterEncoding("UTF-8");
		try {
			AppPager handle = AppPager.handle(pageId, pageCount);
			withdrawApply.setPageId(handle.getPageId());
			withdrawApply.setPageCount(handle.getPageCount());
			List<WithdrawApply> selectWithdrawApplyList=withdrawApplyService.selectWithdrawApply(withdrawApply);
			withdrawApply.setPageCount(null);
			withdrawApply.setPageId(null);
			int num = withdrawApplyService.selectWithdrawApplyNum(withdrawApply);
			Map map =AppPager.getPageInfo(selectWithdrawApplyList, pageCount, num,pageId);
			Result.ok(map, InfoCode.SUCCESS, response);
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	
	
	/**
	 * 退款记录查询导出
	 * @param withdrawApply
	 * @param request
	 * @param response
	 * @return 山治
	 * @throws IOException
	 */
	@RequestMapping(value = "/selectWithdrawApplyExport", method = RequestMethod.POST)
	public void selectWithdrawApplyExport( WithdrawApply withdrawApply,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("退款记录查询导出---withdrawApplyController/selectWithdrawApplyExport");
		request.setCharacterEncoding("UTF-8");
		try {
			String ctxPath = request.getSession().getServletContext().getRealPath("/") + "export/";
			String fileName = "";
			File file = new File(ctxPath.toString());

			if (!file.exists()) {
				file.mkdir();
				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			List<WithdrawApply> selectWithdrawApplyList=withdrawApplyService.selectWithdrawApply(withdrawApply);
			fileName=hhmmSdf1.format(new Date())+".xls";
			 ExportData.exportWithdrawApply(ctxPath + fileName, selectWithdrawApplyList);
		     FileInputStream input=new FileInputStream(ctxPath + fileName);
			 OutputStream output=response.getOutputStream();
			 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
			 byte[] b=new byte[1024*1024];
			 int len=0;
			 while((len=input.read(b))!=-1){
				 output.write(b,0,len);
			 }
			 input.close();
			 output.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return  ;
	}
	
}
